package com.valveinspectionsys.server;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.valveinspectionsys.dbhelp.XmlNameHelper;
import com.valveinspectionsys.httpserver.HttpClientServer;
import com.valveinspectionsys.httpserver.RequestUrl;
import com.valveinspectionsys.util.ActivityStatusUtil;
import com.valveinspectionsys.util.SharedPreferenceUtil;

import android.app.Service;
import android.content.Intent;
import android.location.LocationManager;
import android.os.IBinder;
import android.provider.Settings;
import android.widget.Toast;

public class TimingLocationService  extends Service implements BDLocationListener{

    // 定位帮对象
    public LocationClient client = null;

    // 定时器
    public Timer myTimer = null;

    // 定时任务
    public TimerTask myTimerTask = null;

    // 上传数据服务
    public HttpClientServer server = null;

    // XML帮助 对象
    public SharedPreferenceUtil sh = null;
    
    public ActivityStatusUtil sta;

    public long time;
    
    public LocationManager alm = null;

    @Override
    public void onCreate() {
	server = new HttpClientServer(this);
	sh = new SharedPreferenceUtil(this);
	client = new LocationClient(this);
	sta=new ActivityStatusUtil(this);
	alm=(LocationManager) getSystemService(this.LOCATION_SERVICE);

	initLocation();
	client.start();
	initTimer();

	start();
	super.onCreate();

    }

    @Override
    public void onDestroy() {
	if (myTimer != null) {
	    myTimerTask.cancel();
	    myTimer.cancel();
	}
	if (client != null && client.isStarted()) {
	    client.stop();
	}
	super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {

	return null;
    }

    /**
     * 开启定时器
     */
    public void start() {
	myTimer.schedule(myTimerTask, 1000, 180000);
    }

    /**
     * 初始化定时器及任务
     */
    public void initTimer() {
	if (myTimer == null) {
	    myTimer = new Timer();
	}
	if (myTimerTask == null) {
	    myTimerTask = new TimerTask() {

		@Override
		public void run() {
                      
		    UploadLocation(sh.getValue(XmlNameHelper.GPSLATITUDE), sh.getValue(XmlNameHelper.GPSLONGITUDE));

		}
	    };
	}
    }

   
    /**
     * 初始化定位
     */
    public void initLocation() {
	client = new LocationClient(this);
	LocationClientOption option = new LocationClientOption();
	// 打开GPS
	option.setOpenGps(true);
	// 设置坐标类型
	option.setCoorType("bd09ll");
	//option.setCoorType("gcj02");
	// 设置网络优先
	option.setPriority(LocationClientOption.GpsFirst);
	// 设置产品线名称
	option.setProdName("waterSys");
	// 设置定位定时时间
	option.setScanSpan(5000);
	client.setLocOption(option);
	client.registerLocationListener(this);

    }

    @Override
    public void onReceiveLocation(BDLocation location) {	
	if (location != null) {
	  //  Toast.makeText(getApplicationContext(),(location.getLocType()==61?"GPS":"网络")+"定位精度"+location.getRadius()+"M", Toast.LENGTH_SHORT).show();
	    if (location.getLocType() == 61&&location.hasRadius()&&location.getRadius()<15) {
		sh.insertValue(XmlNameHelper.GPSLATITUDE, location.getLatitude() + "");
		sh.insertValue(XmlNameHelper.GPSLONGITUDE, location.getLongitude() + "");
		sh.insertValue(XmlNameHelper.GPSDATE, System.currentTimeMillis()+"");
	    }else{
		sta.openGPSSettings();
	    }
	}

    }

    @Override
    public void onReceivePoi(BDLocation arg0) {
	// TODO Auto-generated method stub

    }

    /**
     * 上传定位数据
     * 
     * @param latitude
     * @param longitude
     * @return
     */
    public String UploadLocation(String latitude, String longitude) {
   
	Map<String, String> parm = new HashMap<String, String>();

	parm.put("latitude", latitude);
	parm.put("longitude", longitude);	
	parm.put("imsi", sh.getValue(XmlNameHelper.IMEI));
	parm.put("copyerId", sh.getValue(XmlNameHelper.userID));
	String response = server.doPost(parm, RequestUrl.TimingLocation,true);
        System.out.println("------开始上传-------");
	return response;

    }

    /**
     * 判断GPS是否存在或开启
     */
    public boolean openGPSSettings() {

	if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
	    return true;
	}
	Toast.makeText(this, "请开启GPS！", Toast.LENGTH_SHORT).show();
	Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
	startActivity(intent);// 此为设置完成后返回到获取界面
	return false;
    }

}
